$(document).ready(function (){
	view_init();
	event_init();
});

var url_param_id = getQueryString("id");

function view_init()
{
	loadingStart();
	
	$("[data-type=dateAll]").each(function (){
		$(this).datetimepicker({
			  timeFormat: "HH:mm:ss",
	          dateFormat: "yy-mm-dd",
			  monthNames: [ "一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月" ]
		});
	});
	
	list_list(true);
	
	$("#main-content").show();
}

function event_init()
{
	// 查询按钮
	$("#query").click(function (){
		var createTimeStringStart = $("[data-id=createTimeStringStart]").val();
		var createTimeStringEnd = $("[data-id=createTimeStringEnd]").val();
		if (createTimeStringStart && createTimeStringEnd)
		{
			if (moment(createTimeStringStart).isAfter(createTimeStringEnd))
			{
				$("body").umDialog("alert" ,{"content":"开始时间不能大于结束时间"});
				return false;
			}
		}

		var updateTimeStringStart = $("[data-id=updateTimeStringStart]").val();
		var updateTimeStringEnd = $("[data-id=updateTimeStringEnd]").val();
		if (updateTimeStringStart && updateTimeStringEnd)
		{
			if (moment(updateTimeStringStart).isAfter(updateTimeStringEnd))
			{
				$("body").umDialog("alert" ,{"content":"开始时间不能大于结束时间"});
				return false;
			}
		}
		
		var expireTimeStringStart = $("[data-id=expireTimeStringStart]").val();
		var expireTimeStringEnd = $("[data-id=expireTimeStringEnd]").val();
		if (expireTimeStringStart && expireTimeStringEnd)
		{
			if (moment(expireTimeStringStart).isAfter(expireTimeStringEnd))
			{
				$("body").umDialog("alert" ,{"content":"开始时间不能大于结束时间"});
				return false;
			}
		}
		$("#list_tbody").html("");
		list_list(true);
	});
	
	
	// 翻页事件
	index_initPageEvent("list_list()");
}

function list_list(isFirst)
{
	if (isFirst)
	{
		index_pageObj.init();
	}
	
	loadingStart();
	
	var obj = $("#query_panel").umDataBind("serialize" ,"");
	obj.currentPage = index_pageObj.currentPage;
	obj.articleId = getQueryString("id");
	obj.pageSize = index_pageObj.pageSize;
	
	um_ajax_post(
			INDEX_WEBAPP + "/article/comment/select.do",
			obj,
			function (data){
				$("#page_record_count").text(data.size);
				_render(data.articles);
			},
			null);
	function _render(data)
	{
		var buffer = [];
		if (data && data.length >0)
		{
			for (var i=0;i<data.length;i++)
			{
				buffer.push('<tr>');
				buffer.push('<td>'+data[i].commentId+'</td>');
				buffer.push('<td>'+data[i].userName+'</td>');
				buffer.push('<td title="'+data[i].content+'">'+(data[i].content.length > 6 ? data[i].content.substr(0,6) + "..." : data[i].content)+'</td>');
				buffer.push('<td>'+beautyNull(data[i].createTime)+'</td>');
				buffer.push('<td>'+beautyNull(data[i].updateTime)+'</td>');
				buffer.push('<td>'+get_check_str(data[i].check)+'</td>');
				buffer.push('<td>'+beautyNull(data[i].adminName)+'</td>');
				buffer.push('<td style="text-align: center" data-id="'+data[i].commentId+'"><a href="#" data-id=commentS>通过 |</a><a href="#" data-id=commentF> 不通过 '+
					'|<a href="#" data-id=deleteF> 删除'+'</td>');
				buffer.push('<tr>');
			}
			$("#list_tbody").html(buffer.join(""));
		}
		else
		{
			index_pageObj.toEndPage();
		}

		index_pageObj.isEndMethod(data);

	}
}

function get_check_str(checkCode)
{
	if (checkCode == 0)
	{
		return "未审核";
	}
	else if (checkCode == 1)
	{
		return "审核通过";
	}
	else
	{
		return "审核未通过";
	}
}


//编辑按钮
$("#list_tbody").delegate("[data-id=commentS]" ,"click" ,function (){
	
	url = "/article/comment/update.do";
	var obj = new Object();
	obj.commentId =$(this).parent().attr("data-id");
	obj.check = 1;
	um_ajax_post(
			url,
			obj,
			function (data){
				window.location.href = "/article/direction/comment.do?id="+getQueryString("id");
			},
			null);
});

//编辑按钮
$("#list_tbody").delegate("[data-id=commentF]" ,"click" ,function (){
	
	url = "/article/comment/update.do";
	var obj = new Object();
	obj.commentId =$(this).parent().attr("data-id");
	obj.check = 2;
	um_ajax_post(
			url,
			obj,
			function (data){
				window.location.href = "/article/direction/comment.do?id="+getQueryString("id");
			},
			null);
});

// 删除按钮
$("#list_tbody").delegate("[data-id=deleteF]" ,"click" ,function (){
	
	url = "/article/comment/delete.do";
	var obj = new Object();
	obj.commentId =$(this).parent().attr("data-id");
	obj.articleId = getQueryString("id");
	um_ajax_post(
			url,
			obj,
			function (data){
				// $("body").umDialog("info" ,{"content":"删除成功",left:"40%"});
				window.location.href = "/article/direction/comment.do?id="+getQueryString("id");
			},
			null);
});

// 删除全部评论
$("#delete").click(function (){
	url = "/article/comment/deleteTotal.do";
	var obj = new Object();
	obj.articleId = getQueryString("id");
	um_ajax_post(
			url,
			obj,
			function (data){
				// $("body").umDialog("info" ,{"content":"删除成功",left:"40%"});
				window.location.href = "/article/direction/index.do";
			},
			null);
});